package com.design.mode.adapter.demo;

import java.io.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * 适配器
 * Created by lucky on 2018/9/14.
 */
public class FilePropeties extends Properties implements FileIo {

    private Properties properties = new Properties();

    public void writeFile(String name) {
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(name);
            properties.store(outputStream,"@author : zflu");
        }catch (Exception e){

        }finally {
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void readFile(String name) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(name));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            properties.load(fileInputStream);
            Iterator<Map.Entry<Object, Object>> iterator = properties.entrySet().iterator();
            while (iterator.hasNext()){
                Map.Entry<Object, Object> next = iterator.next();
                System.out.println(next.getKey() + ": " + next.getValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void setValue(String key, String value) {
        properties.setProperty(key,value);
    }

    public String getValue(String key) {
        return properties.getProperty(key);
    }
}
